package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.analyse.JSoupUtils;
import com.kdtech.crawler.CrawlHTML;
import com.kdtech.crawler.at.UrlArgumentTop;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.HtmlCleaner;
import com.kdtech.utils.NumberUtils;
import com.kdtech.utils.RegexUtils;
import com.kdtech.utils.StringUtils;
import com.kdtech.utils.HtmlCleaner;

/**
 * <新闻 - 大众网> 解析类
 *
 * @author KK
 *
 */
public class DzwwwNewsAnalyse implements AnalyseNews {

	
	public boolean isDetailPage(String url) {
		String[] regex={
				"http://[a-z]*.dzwww.com/.*/[0-9]{6}/t[0-9]{8}_[0-9]*.htm[l]?",
				};
		return RegexUtils.matchAnyIgnoreCase(url, regex);
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		if (urlMeta.getHtml() == null) {
		}
		String htmltxt=urlMeta.getHtml();
		String url=urlMeta.getUrl();

		String title=null;
		String content=null;
		Long date=null;
		String dateStr=null;
		String commnetNum=null;
		String clickNum=null;
		Document doc=Jsoup.parse(htmltxt);


		doc.select(".ad,.sidebar,#zyql_scenic_ad").remove();

		title=doc.select("div.content h1").text();
		content =HtmlCleaner.getContentHtml(url, doc.select("div.content  div.xiangxi"));
		date=DateUtils.matchDate(doc.select("div.content  div.wenzi > ul  ").text());
		if(StringUtils.isNullOrEmpty(title)){
			title=doc.select("div#news-header  h1").text();
			date=DateUtils.matchDate(doc.select("div#news-header > div.infor > div.left > span").text());
			content=HtmlCleaner.getContentHtml(url,doc.select("div#news-header  div.summary,div#news-con"));
		}
		if(StringUtils.isNullOrEmpty(title)){
			title=doc.select("div#news-con  h1").text();
			date=DateUtils.matchDate(doc.select(" div#news-con  div.top ").text());
			content =HtmlCleaner.getContentHtml(url, doc.select("div#news-con  div.con"));
		}
		if(StringUtils.isNullOrEmpty(title)){
			title=doc.select("div#news-header h1").text();
			date=DateUtils.matchDate(doc.select("div#news-header div.infor").text());
			content =HtmlCleaner.getContentHtml(url, doc.select("#content"));
		}
		if(StringUtils.isNullOrEmpty(title)){
			title=doc.select("span.dabiaotu strong").text();
			date=DateUtils.matchDate(doc.select("span.huiriqi").text());
			content =HtmlCleaner.getContentHtml(url, doc.select("#content"));
		}

		if(StringUtils.isNullOrEmpty(title) || StringUtils.isNullOrEmpty(dateStr)){
			title=doc.select("span.dabiaotu strong").text();
			date=DateUtils.matchDate(doc.select("td.zhengweihei").text());
			content=HtmlCleaner.getContentHtml(url,doc.select("#DOCHTMLCON"));
		}

		if(StringUtils.isNullOrEmpty(title)){
			title=doc.select("div#news-con > div.top h1").text();
			date=DateUtils.matchDate(doc.select("div#news-con > div.top p").text());
			content=HtmlCleaner.getContentHtml(url,doc.select("div.TRS_Editor"));
		}

		if(StringUtils.isNullOrEmpty(title)){
			title=doc.select("title").text();
			title=StringUtils.substringBefore(title,"_");
		}

		if (StringUtils.isBlank(content)) {
			content=HtmlCleaner.getContentHtml(url,doc.select("#photoDesc"));
		}

		if (date ==null){
			date=JSoupUtils.matchDate(doc,"作者","来源");
		}

		String author=JSoupUtils.matchAuthor(doc, "来源：");

		NewsMeta news=new NewsMeta();
		news.setUrl(url);
		news.setType(1);
		news.setTitle(StringUtils.trimSpace(title));
		news.setCommentNum(NumberUtils.parseInt(commnetNum));
		news.setClickNum(NumberUtils.parseInt(clickNum));
		news.setContent(StringUtils.trimSpace(content));
		news.setDate(date);
		news.setAuthor(author);

		return news;

	}

	public static void main(String[] args) {
		DzwwwNewsAnalyse a=new DzwwwNewsAnalyse();
//		String url="http://www.dzwww.com/bzgg/200904/t20090408_4393739.htm";
//		String url="http://home.dzwww.com/zx/red/200805/t20080519_3554919.htm";
		String url="http://linyi.dzwww.com/tyyl/201404/t20140418_10073034.htm";
//		String url="http://dzwww.com/special/201208/t20120817_7343217.htm";
//		String url="http://www.dzwww.com/special/ts/jdf/201211/t20121122_7723722.htm";
//		String url="http://www.dzwww.com/xinwen/guojixinwen/201211/t20121122_7723418.htm";
//		String url="http://www.dzwww.com/xinwen/shehuixinwen/201211/t20121122_7723428.htm";
//		String url="http://www.dzwww.com/xinwen/guoneixinwen/201211/t20121122_7723748.htm";

		System.out.println(a.isDetailPage(url));
		url=UrlArgumentTop.FromatUrl(url);
		System.err.println(url);
		System.err.println(a.isDetailPage(url));
		UrlMeta meta=CrawlHTML.responseToURL(url);
		NewsMeta parserHtml=a.parserHtml(meta);
		System.out.println(parserHtml);
	}

	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}
	
	public boolean isNeedUpdate(){
		return false;
	}
}
